package cm.studapp.actIndo;

import cm.studapp.R;
import cm.studapp.R.layout;
import cm.studapp.database.DbManager;
import cm.studapp.helpers.ButtonHelper;
import cm.studapp.helpers.TimeHelper;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;
import android.widget.TimePicker;
import cm.studapp.InitStudApp;

public class ActRepStudy extends Activity implements OnClickListener, android.content.DialogInterface.OnClickListener {

	private Button addButton;
	private ButtonHelper bHelper;
	private DbManager db;
	private TimeHelper th;
	private Cursor signatureData;
	private int idAct;
	private long fecha1;
	private long fecha2;
	private LinearLayout parent;
	private int idDuty = -1;
	public ActRepStudy() {
		// TODO Auto-generated constructor stub
	}
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.rep_act_homeworks);
		idAct=getIntent().getExtras().getInt("idAct");
		init();
		initComponents();
		initViews();
	}
	
	public void onResume(){
		super.onResume();
		getHomeworks();
	}
	
	private void getHomeworks() {
		
		// TODO Auto-generated method stub
		parent.removeAllViews();
		Cursor hw = db.getHomeWorks(idAct, 1);
		
		while(hw.moveToNext()){
			RelativeLayout child = (RelativeLayout)getLayoutInflater().inflate(R.layout.rep_act_duty_elem, null);
			child.setOnClickListener(this);
			LinearLayout.LayoutParams paramChild = new
					LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
			paramChild.leftMargin = 10;
			paramChild.rightMargin = 10;
			paramChild.bottomMargin = 2;
			child.setLayoutParams(paramChild);
			
			child.setId(hw.getInt(0));
			
			TextView nombre = (TextView)child.getChildAt(0);
			nombre.setText(hw.getString(3));			
			
			TextView dia = (TextView)child.getChildAt(4);
			dia.setText(th.millisToHumanDay(hw.getLong(4))+"  "+th.millisToHumanDate(hw.getLong(4)));
			
			TextView initHour = (TextView)child.getChildAt(3);
			initHour.setText(th.millisToHour(hw.getLong(4)));
			
			TextView endHour = (TextView)child.getChildAt(1);
			endHour.setText(th.millisToHour(hw.getLong(5)));			
				
			
			parent.addView(child);
		}
		
	}

	private void initViews() {
		// TODO Auto-generated method stub
		/*
		DatePicker dpSd=(DatePicker)findViewById(R.id.ApptAddDP_startDate);
		TimePicker tpst=(TimePicker)findViewById(R.id.ApptAddTP_startTime);
		TimePicker tpft=(TimePicker)findViewById(R.id.ApptAddTP_endTime);
		//EditText name=(EditText)findViewById(R.id.ApptAddET_hwkName);
//		fecha1=TimeHelper.fullDateToMillis(dpSd, tpst);
//		fecha2=TimeHelper.fullDateToMillis(dpSd, tpft);
		*/
		parent = (LinearLayout)findViewById(R.id.RepActHomeWoLL_homeworkParent);
		TextView nombreTipo=(TextView)findViewById(R.id.RepActHomeWoTW_actType);
		nombreTipo.setText("Horarios de estudios" );
		
		TextView nombreAct = (TextView)findViewById(R.id.RepActHomeWoTW_actName);
		nombreAct.setText(db.getActName(idAct));

	}

	public void init(){
		bHelper = new ButtonHelper(this);
		addButton = (Button)findViewById(R.id.RepActHomeWoBTN_addHomework);
		addButton.setId(9379);
		addButton.setOnClickListener(bHelper);
		
	}
	
	public void initComponents(){
		db=new DbManager(this);
		signatureData=db.getRepActData(idAct);
		th = new TimeHelper();
		bHelper = new ButtonHelper(this);
		
	}
	
	public int getId(){
		return idAct;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
   /*
		idDuty=v.getId();
		// 1. Instantiate an AlertDialog.Builder with its constructor
		AlertDialog.Builder builder = new AlertDialog.Builder(this);

		// 2. Chain together various setter methods to set the dialog characteristics
		builder.setMessage("Terminaste la wea?");
		builder.setPositiveButton("Ti", this);
		builder.setNegativeButton("Nope", this);
		// 3. Get the AlertDialog from create()
		AlertDialog dialog = builder.create();
		dialog.show();
		*/
	}

	@Override
	public void onClick(DialogInterface dialog, int which) {
		// TODO Auto-generated method stub
		if(which == dialog.BUTTON_POSITIVE){
			System.out.println("Apretado el boton positivo, id de la tarea:"+idDuty);
			db.insertStatistics(idDuty,1);
		}
		else{
			db.insertStatistics(idDuty,0);
		}
	}

}
